﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Family.Core;
using Family.Core.DataInterfaces;
using Family.Core.Model;
using Family.Data.DBRepositories;
using Family.Data.Factories;

public partial class Controls_DayStatisticControl : UserControl
{
    #region Private place

    private const String ORDER_KEY = "OrderKey";
    private const String CATEGORY_KEY = "CategoryKey";

    private OrderRepository oRepo;
    private IRepository<OrderItem> oiRepo;

    private Int32 CurrentOrderID
    {
        get
        {
            return ViewState[ORDER_KEY] == null ? 0 : (Int32)ViewState[ORDER_KEY];
        }
        set { ViewState[ORDER_KEY] = value; }
    }

    private Int32 CurrentCategoryID
    {
        get
        {
            return ViewState[CATEGORY_KEY] == null ? 0 : (Int32)ViewState[CATEGORY_KEY];
        }
        set { ViewState[CATEGORY_KEY] = value; }
    }

    private void BindOrderForCategory(ListView lv, Int32 orderId, Int32 categoryId)
    {
        lv.DataSource = oRepo.GetItemsForOrderByCategoryId(orderId, categoryId);
        lv.DataBind();
    }

    #endregion

    #region Controls' events

    protected Controls_DayStatisticControl()
    {
        oRepo = new OrderRepository(WebFactory.CurrentSession);
        oiRepo = new Repository<OrderItem>(WebFactory.CurrentSession);
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void lvDays_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        var lv = e.Item.FindControl("lvItems") as ListView;
        var lblName = e.Item.FindControl("lblName") as Label;
        if (lv != null && lblName != null)
        {
            var category = e.Item.DataItem as Category;
            lblName.Text = String.Format("{0} ({1})", category.Name, oRepo.GetAmountByCategoryForOrder(CurrentOrderID, category.Id).ToString("N2"));
            BindOrderForCategory(lv, CurrentOrderID, category.Id);
        }
    }

    protected void lvItems_Canceling(object sender, ListViewCancelEventArgs e)
    {
        var lv = sender as ListView;
        if (lv != null)
        {
            lv.EditIndex = -1;
            BindOrderForCategory(lv, CurrentOrderID, CurrentCategoryID);
        }
    }

    protected void lvItems_Editing(object sender, ListViewEditEventArgs e)
    {
        var lv = sender as ListView;
        if (lv != null)
        {
            var oi = oiRepo.Get((Int32) lv.DataKeys[e.NewEditIndex].Value);
            CurrentCategoryID = oi.Product.Category.Id;
            lv.EditIndex = e.NewEditIndex;
            BindOrderForCategory(lv, CurrentOrderID, CurrentCategoryID);
        }
    }

    protected void lvItems_Deleting(object sender, ListViewDeleteEventArgs e)
    {
        var lv = sender as ListView;
        if (e.ItemIndex >= 0 && lv != null)
        {
            var oi = oiRepo.Get((Int32)lv.DataKeys[e.ItemIndex].Value);
            CurrentCategoryID = oi.Product.Category.Id;
            oiRepo.Delete(oi);
            BindOrder(oRepo.Get(CurrentOrderID));
        }
    }

    protected void lvItems_Updating(object sender, ListViewUpdateEventArgs e)
    {
        var lv = sender as ListView;
        if (lv != null)
        {
            var txt = lv.Items[e.ItemIndex].FindControl("txtAmount") as TextBox;
            if (txt != null)
            {
                var oi = oiRepo.Get((Int32) lv.DataKeys[e.ItemIndex].Value);
                oi.Amount = Decimal.Parse(txt.Text);
                oiRepo.SaveOrUpdate(oi);
                lv.EditIndex = -1;
                BindOrderForCategory(lv, CurrentOrderID, CurrentCategoryID);
            }
        }
        
    }

    #endregion

    #region Public methods

    public void BindOrder(Order order)
    {
        if (order != null)
        {
            CurrentOrderID = order.Id;
            lvDays.DataSource = oRepo.GetCategoriesByOrderId(CurrentOrderID);
            lblSum.Text = oRepo.GetAmountByDate(order.Date).ToString("N2");
            lblDate.Text = order.Date.ToString("dd-MM-yyyy");
        }
        else
        {
            lvDays.DataSource = null;
        }
        
        lvDays.DataBind();
    }

    #endregion
}